package org.example.nettydemo.netty.udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liujing
 */
public class UdpServer {
    private static final Logger logger = LoggerFactory.getLogger(UdpServer.class);

    public static void main(String[] args) {
//        EventLoopGroup bossGroup=new NioEventLoopGroup(1);
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap
                    .group(bossGroup)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_RCVBUF, 1024 * 1024 * 50)
                    .option(ChannelOption.SO_REUSEADDR,true)
                    .handler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(Channel channel) throws Exception {
                            ChannelPipeline pipeline = channel.pipeline();
                            pipeline.addLast(new UdpServerHandler());
                        }
                    });
            ChannelFuture channelFuture= bootstrap.bind(10007).sync();
            logger.info("服务器启动成功，监听端口 10007");
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
        }

    }
}
